/*
 *  Copyright (C) 2010  Noxious Productions
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "button.hpp"

Button::Button()
{
    parent = NULL;
    imageNormal = NULL;
    imageHover = NULL;
    imagePressed = NULL;
    text = NULL;
}

Button::~Button()
{
    if (imageNormal)
        delete imageNormal;
    if (imageHover)
        delete imageHover;
    if (imagePressed)
        delete imagePressed;
    if (text)
        delete text;
}

void Button::SetParent(sf::RenderWindow *renderWindow)
{
    parent = renderWindow;
}

void Button::Draw()
{
    if (!parent)
        return;

    parent->Draw(sprite);
    if (text)
        parent->Draw(*text);
}

bool Button::SetImageNormal(const std::string &fileName)
{
    imageNormal = new sf::Image;
    if (!imageNormal->LoadFromFile(fileName))
        return false;
    SetState(Normal);
    CenterText();
    return true;
}

bool Button::SetImageHover(const std::string &fileName)
{
    imageHover = new sf::Image;
    if (!imageHover->LoadFromFile(fileName))
        return false;
    CenterText();
    return true;
}

bool Button::SetImagePressed(const std::string &fileName)
{
    imagePressed = new sf::Image;
    if (!imagePressed->LoadFromFile(fileName))
        return false;
    CenterText();
    return true;
}

bool Button::CheckEvents(sf::Event &Event)
{
    if (Event.Type == sf::Event::MouseMoved)
    {
        sf::IntRect rect((int)sprite.GetPosition().x, (int)sprite.GetPosition().y, (int)(sprite.GetPosition().x + sprite.GetSize().x), (int)(sprite.GetPosition().y + sprite.GetSize().y));

        if (rect.Contains(Event.MouseMove.X, Event.MouseMove.Y))
        {
            if (state == Normal)
                SetState(Hover);
        }
        else
        {
            if (state == Hover)
                SetState(Normal);
        }        
    }
    else if (Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
    {
        sf::IntRect rect((int)sprite.GetPosition().x, (int)sprite.GetPosition().y, (int)(sprite.GetPosition().x + sprite.GetSize().x), (int)(sprite.GetPosition().y + sprite.GetSize().y));

        if (rect.Contains(Event.MouseButton.X, Event.MouseButton.Y))
        {
            SetState(Pressed);
        }
    }
    else if (Event.Type == sf::Event::MouseButtonReleased && Event.MouseButton.Button == sf::Mouse::Left)
    {
        if (state == Pressed)
        {
            sf::IntRect rect((int)sprite.GetPosition().x, (int)sprite.GetPosition().y, (int)(sprite.GetPosition().x + sprite.GetSize().x), (int)(sprite.GetPosition().y + sprite.GetSize().y));

            if (rect.Contains(Event.MouseButton.X, Event.MouseButton.Y))
                SetState(Hover);
            else
                SetState(Normal);

            return true;
        }
    }

    return false;
}

bool Button::SetText(const string &Text, const sf::Font &Font, float size)
{
    if (!text)
    {
        text = new sf::String(Text, Font, size);
        if (!text)
            return false;
    }
    else
    {
        text->SetText(Text);
        text->SetFont(Font);
        text->SetSize(size);
    }

    CenterText();
    return true;
}

void Button::SetPosition(float X, float Y)
{
    sprite.SetPosition(X, Y);
    CenterText();
}

void Button::SetState(State s)
{
    state = s;
    switch (s)
    {
    case Normal:
        if (imageNormal)
            sprite.SetImage(*imageNormal);
        break;
    case Hover:
        if (imageHover)
            sprite.SetImage(*imageHover);
        else if (imageNormal)
            sprite.SetImage(*imageNormal);
        break;
    case Pressed:
        if (imagePressed)
            sprite.SetImage(*imagePressed);
        else if (imageNormal)
            sprite.SetImage(*imageNormal);
        break;
    }
}

void Button::CenterText()
{
    if (text)
        text->SetPosition(sprite.GetPosition().x + (sprite.GetSize().x / 2 - text->GetRect().GetWidth() / 2), sprite.GetPosition().y + (sprite.GetSize().y / 2 - text->GetRect().GetHeight() / 2) - 2);
}
